﻿using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using Microsoft.Office.Interop.Outlook;
using Outlook.WorkItems.Extension.Addin.Forms;
using Microsoft.TeamFoundation.Client;

namespace Outlook.WorkItems.Extension.Addin.Actions
{
    public class InsertWorkItemInformationsAction : IBowieAction
    {
        private readonly Attachments _attachments;
        public string WorkItemInformations { get; set; }

        public InsertWorkItemInformationsAction(Attachments attachments)
        {
            _attachments = attachments;
        }

        public void Do()
        {
            using (var frm = new InsertWorkItemDataSettingsForm())
            {
                if (frm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    return;

                var workItems = frm.SelectedWorkItems;
                var fieldsToInsert = frm.FieldsToInsert;
                var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(frm.ServerUri));
                var tswa = tfs.GetService<TswaClientHyperlinkService>();

                WorkItemInformations = fieldsToInsert.Aggregate(@"<table class=MsoTableMediumGrid3Accent6 border=1 cellspacing=0 cellpadding=0 style='border-collapse:collapse;border:none;mso-border-alt:solid white 1.0pt;mso-border-themecolor:background1;mso-yfti-tbllook:1184;mso-padding-alt:0cm 5.4pt 0cm 5.4pt'>
                                                            <tr style='mso-yfti-irow:-1;mso-yfti-firstrow:yes'>", (current, column) => current + (@"<td  valign=top style='border:solid white 1.0pt;mso-border-themecolor:background1;border-bottom:solid white 3.0pt;mso-border-bottom-themecolor:background1;background:#F79646;mso-background-themecolor:accent6;padding:0cm 5.4pt 0cm 5.4pt'>
                                                                <p class=MsoNormal style='mso-yfti-cnfc:5'><span class=SpellE><b><span style='font-size:12.0pt;font-family:""Times New Roman"",""serif"";color:windowtext;mso-fareast-language:FR'>" + column.Name + @"</span></b></span><b><span style='font-size:12.0pt;font-family:""Times New Roman"",""serif"";color:windowtext;mso-fareast-language:FR'><o:p></o:p></span></b></p></td>"));

                WorkItemInformations += "</tr>";

                foreach (var wi in workItems)
                {
                    WorkItemInformations += "<tr style='mso-yfti-irow:0'>";
                    foreach (var column in fieldsToInsert)
                    {
                        if (wi.Fields.Contains(column.Name))
                        {
                            if (tswa != null && column.Name.ToLower() == "id")
                            {
                                try
                                {
                                    var wiUri = tswa.GetWorkItemEditorUrl(wi.Id);

                                    WorkItemInformations += @"<td valign=top style='border-top:none;border-left:none;border-bottom:solid white 1.0pt;
                                                  mso-border-bottom-themecolor:background1;border-right:solid white 1.0pt;
                                                  mso-border-right-themecolor:background1;mso-border-top-alt:solid white .75pt;
                                                  mso-border-top-themecolor:background1;mso-border-left-alt:solid white .75pt;
                                                  mso-border-left-themecolor:background1;mso-border-alt:solid white .75pt;
                                                  mso-border-themecolor:background1;mso-border-right-alt:solid white 1.0pt;
                                                  mso-border-right-themecolor:background1;background:#FDE4D0;mso-background-themecolor:
                                                  accent6;mso-background-themetint:63;padding:0cm 5.4pt 0cm 5.4pt'>
                                                  <p class=MsoNormal><span style='font-size:12.0pt;font-family:""Times New Roman"",""serif"";
                                                  color:windowtext;mso-fareast-language:FR'><a href='" + wiUri.AbsoluteUri + "'>" + wi.Fields[column.Name].Value + @"</a><o:p></o:p></span></p>
                                                  </td>";
                                }
                                catch
                                {
                                    WorkItemInformations += @"<td valign=top style='border-top:none;border-left:none;border-bottom:solid white 1.0pt;
                                                  mso-border-bottom-themecolor:background1;border-right:solid white 1.0pt;
                                                  mso-border-right-themecolor:background1;mso-border-top-alt:solid white .75pt;
                                                  mso-border-top-themecolor:background1;mso-border-left-alt:solid white .75pt;
                                                  mso-border-left-themecolor:background1;mso-border-alt:solid white .75pt;
                                                  mso-border-themecolor:background1;mso-border-right-alt:solid white 1.0pt;
                                                  mso-border-right-themecolor:background1;background:#FDE4D0;mso-background-themecolor:
                                                  accent6;mso-background-themetint:63;padding:0cm 5.4pt 0cm 5.4pt'>
                                                  <p class=MsoNormal><span style='font-size:12.0pt;font-family:""Times New Roman"",""serif"";
                                                  color:windowtext;mso-fareast-language:FR'>" + wi.Fields[column.Name].Value + @"<o:p></o:p></span></p>
                                                  </td>";
                                }
                            }
                            else
                            {
                                WorkItemInformations += @"<td valign=top style='border-top:none;border-left:none;border-bottom:solid white 1.0pt;
                                                  mso-border-bottom-themecolor:background1;border-right:solid white 1.0pt;
                                                  mso-border-right-themecolor:background1;mso-border-top-alt:solid white .75pt;
                                                  mso-border-top-themecolor:background1;mso-border-left-alt:solid white .75pt;
                                                  mso-border-left-themecolor:background1;mso-border-alt:solid white .75pt;
                                                  mso-border-themecolor:background1;mso-border-right-alt:solid white 1.0pt;
                                                  mso-border-right-themecolor:background1;background:#FDE4D0;mso-background-themecolor:
                                                  accent6;mso-background-themetint:63;padding:0cm 5.4pt 0cm 5.4pt'>
                                                  <p class=MsoNormal><span style='font-size:12.0pt;font-family:""Times New Roman"",""serif"";
                                                  color:windowtext;mso-fareast-language:FR'>" + wi.Fields[column.Name].Value + @"<o:p></o:p></span></p>
                                                  </td>";
                            }
                        }
                        else
                        {
                            WorkItemInformations += @"<td valign=top style='border-top:none;border-left:none;border-bottom:solid white 1.0pt;
                                                  mso-border-bottom-themecolor:background1;border-right:solid white 1.0pt;
                                                  mso-border-right-themecolor:background1;mso-border-top-alt:solid white .75pt;
                                                  mso-border-top-themecolor:background1;mso-border-left-alt:solid white .75pt;
                                                  mso-border-left-themecolor:background1;mso-border-alt:solid white .75pt;
                                                  mso-border-themecolor:background1;mso-border-right-alt:solid white 1.0pt;
                                                  mso-border-right-themecolor:background1;background:#FDE4D0;mso-background-themecolor:
                                                  accent6;mso-background-themetint:63;padding:0cm 5.4pt 0cm 5.4pt'>
                                                  <p class=MsoNormal><span style='font-size:12.0pt;font-family:""Times New Roman"",""serif"";
                                                  color:windowtext;mso-fareast-language:FR'><o:p></o:p></span></p>
                                                  </td>";
                        }
                    }
                    WorkItemInformations += "</tr>";

                    if (frm.AttachWorkItemAttachment != true)
                        continue;

                    foreach (Microsoft.TeamFoundation.WorkItemTracking.Client.Attachment att in wi.Attachments)
                    {
                        var wclient = new WebClient { UseDefaultCredentials = true };
                        var buffer = wclient.DownloadData(att.Uri);
                        var localPath = Path.GetTempPath() + "\\" + att.Name;
                        File.WriteAllBytes(localPath, buffer);
                        _attachments.Add(localPath, OlAttachmentType.olByValue, Missing.Value, att.Name);
                    }
                }
                WorkItemInformations += "</table>";
            }

        }
    }
}
